jQuery.validator.addMethod("checkBalance", function (value, element) {
    var balance = 0;
    return this.optional(element) || value > balance;
}, $.validator.format("数值必须大于0"));


//必输字段 面额
$("#amount").blur(function () {
    if (this.value != 0) {
        $("#message").css('display', 'none');
    } else {
        $(this).next("span").html("为必填字段");
    }
});
//必输字段 主题名称
$("#topicName").blur(function () {
    if (this.value != 0) {
        $("#message0").css('display', 'none');
    } else {
        $(this).next("span").html("为必填字段");
    }
});
//必输字段 数量
$("#totalCount").blur(function () {
    if (this.value != 0) {
        $("#message2").css('display', 'none');
    } else {
        $(this).next("span").html("为必填字段");
    }
});

//spu sku 文本框校验
function testShuRu(a) {
    var patt1 = new RegExp("^[;.a-zA-Z0-9\u0020]*$");
    if (!patt1.test(a)) {
        $.Huimodalalert('请按商品编码组成规则：输入字母、数字、英文状态下分号、点', 2000);
        // $("#spuText").val().replace(/[^;.a-zA-Z0-9]/g, "");
    }
}

function getTopicType() {
    if ($("#topicType").val() == 20) {
        $("#orderMoney").html("单笔订单运费金额满");
        $("#applRange").hide();
        $("#applCate").hide();
        $("#cateLevel").hide();
        $("#tab_goods").hide();
    } else {
        $("#orderMoney").html("单笔订单适用商品金额满");
        $("#applRange").show();
        $("#applCate").show();
        $("#cateLevel").show();
        $("#tab_goods").show();
    }
}

var applPaidType = '';
$("input[name='paidType']").change(function () {
    applPaidType = '';
    var groupCheckbox = $("input[name='paidType']");
    for (var i = 0; i < groupCheckbox.length; i++) {
        if (groupCheckbox[i].checked) {
            var val = groupCheckbox[i].value;
            if (i == groupCheckbox.length - 1) {
                applPaidType += val;
            } else {
                applPaidType += val + ',';
            }
        }
        if (groupCheckbox[i].checked == false) {
            document.getElementById("applPaidType0").checked = false;
        }
    }
    console.log(applPaidType);

});


var count = 0;//默认 没有
function check_pay() {
    if (count == 0) {
        document.getElementById("applPaidType0").checked = true;
        $("input[name='paidType']").prop("checked", true);
        count = 1;
        applPaidType = document.getElementById("applPaidType0").value;
        console.log(applPaidType);
    } else if (count == 1) {
        document.getElementById("applPaidType0").checked = false;
        $("input[name='paidType']").prop("checked", false);
        count = 0;
        applPaidType = '';
    }
}

var checkLength = function (dom, maxLength) {
    var l = 0;
    for (var i = 0; i < dom.value.length; i++) {
        if (/[\u4e00-\u9fa5]/.test(dom.value[i])) {
            l += 2;
        } else {
            l++;
        }
        if (l > maxLength) {
            dom.value = dom.value.substr(0, i);
            break;
        }
    }
};


function putDataToPage() {
    /*渲染spu sku值到页面*/
    var formData = new FormData();
    // formData.append("topicId", 100);
    // formData.append("applProduct", 1);
    // if($("#tabbox1")[0].style.display == "block"){
    //     formData.append('type',1);
    // }
    // else if($("#tabbox2")[0].style.display == "block"){
    //     formData.append('type',2);
    // }
    // formData.append('spulist',$('#spuText').val());
    // formData.append('skulist',$('#skuText').val());
    // formData.append('spulist1',$('#spuText1').val());
    // formData.append('skulist1',$('#skuText1').val());
    formData.append('file', $("#fileUp")[0].files[0]);
    formData.append('areaId', 1);
    // console.log($("#fileUp")[0].value)
    $.ajax({
        url: root.dockingplatform.excelImportProduct,
        cache: false,  //禁用缓存
        type: "POST",
        // 告诉jQuery不要去处理发送的数据
        processData: false,
        // 告诉jQuery不要去设置Content-Type请求头
        contentType: false,
        data: formData,
        success: function (data) {
            if (data.code == 1) {
                $.Huimodalalert(data.message, 2000);
            }
            var spuStr = "";
            var skuStr = "";
            var oldSpuStr = $("#spuText").val();
            var oldSkuStr = $('#skuText').val();
            var oldSpuStr1 = $("#spuText1").val();
            var oldSkuStr1 = $('#skuText1').val();

            var SpuStr = data.spuCodes;
            var SkuStr = data.skuCodes;


            if ($('.current').text() == '适用商品') {
                $("#spuText").val(oldSpuStr + SpuStr);
                $("#skuText").val(oldSkuStr + SkuStr);
            }
            if ($('.current').text() == '排除商品') {
                $("#spuText1").val(oldSpuStr1 + SpuStr);
                $("#skuText1").val(oldSkuStr1 + SkuStr);
            }
            $("#fileUp")[0].value = ""


            // var oldSpuStr1 = $("#spuText1").val();
            // var oldSkuStr1 = $('#skuText1').val();
            // for (var i = 0; i < data.successList.length; i++) {
            //     if (i < data.successList.length - 1) {
            //         if (data.successList[i].spuCode!=null){
            //             spuStr = data.successList[i].spuCode+ "  ;  "+spuStr;
            //         }
            //         if (data.successList[i].skuCode!=null){
            //             skuStr = data.successList[i].skuCode + "  ;  "+skuStr;
            //         }
            //     }
            //     else if (i == data.successList.length - 1) {
            //         if (data.successList[i].spuCode!=null){
            //             spuStr += data.successList[i].spuCode;
            //         }
            //         if (data.successList[i].skuCode!=null){
            //             skuStr += data.successList[i].skuCode;
            //         }
            //     }
            // }
            //
            // // if (skuStr.lastIndexOf(";")==(skuStr.length-1)){
            // //     $("#skuText").val(skuStr.substring(0,skuStr.length-1));
            // // }
            // if($("#tabbox1")[0].style.display == "block"){
            //     if($("#spuText").val()==''){
            //         $("#spuText").val(spuStr);
            //     }
            //     else{
            //         if(spuStr!=''){
            //             $("#spuText").val(oldSpuStr+';'+spuStr);
            //         }
            //     }
            //     if($("#skuText").val()==''){
            //         $("#skuText").val(skuStr);
            //     }
            //     else{
            //         if(skuStr!=''){
            //             console.log('oldSkuStr'+':'+oldSkuStr);
            //             $("#skuText").val(oldSkuStr+';'+skuStr);
            //         }
            //     }
            // }
            // else if($("#tabbox2")[0].style.display == "block"){
            //     if($("#spuText1").val()==''){
            //         $("#spuText1").val(spuStr);
            //     }
            //     else{
            //         if(spuStr!=''){
            //             $("#spuText1").val(oldSpuStr+';'+spuStr);
            //         }
            //     }
            //     if($("#skuText1").val()==''){
            //         $("#skuText1").val(skuStr);
            //     }
            //     else{
            //         if(skuStr!=''){
            //             console.log('oldSkuStr'+':'+oldSkuStr);
            //             $("#skuText1").val(oldSkuStr+';'+skuStr);
            //         }
            //     }
            // }

            //
            // var p=[];
            // var k=[];
            // var u=[];
            // var o=[];
            // var failSpu=[];
            // var failSku=[];
            // for(var i=0;i<data.successList.length;i++){
            //     var j=i+1;
            //     if(data.successList[i].spuCode==null){
            //         p.push(j);
            //     }
            //     if(data.successList[i].skuCode==null){
            //         k.push(j);
            //     }
            //     if(data.successList[i].spuCode!=null){
            //         u.push(j);
            //     }
            //     if(data.successList[i].skuCode!=null){
            //         o.push(j);
            //     }
            // };
            //
            // var q=u.length+o.length;
            // if(data.failSpuId.length == 0 && data.failSkuId.length == 0){
            //     if($('.current').text()=='适用商品'){
            //         $('#applNumber').html(data.successList.length);
            //     }
            //     if($('.current').text()=='排除商品'){
            //         $('#removeNumber').html(data.successList.length);
            //     }
            //     if(data.successList.length == 0){
            //         $.Huimodalalert('导入的表中没有商品数据,请检查数据表!',2000);
            //     }else{
            //         $.Huimodalalert('成功导入全部商品',2000);
            //     }
            // }
            // else if(data.failSpuId.length != 0 && data.failSkuId.length != 0){
            //     console.log(123)
            //     for(var i=0;i<data.failSpuId.length;i++){
            //         failSpu.push(data.failSpuId[i]);
            //     };
            //     for(var i=0;i<data.failSkuId.length;i++){
            //         failSku.push(data.failSkuId[i]);
            //     };
            //     if($('.current').text()=='适用商品'){
            //         $('#applNumber').html(data.successList.length);
            //     }
            //     if($('.current').text()=='排除商品'){
            //         $('#removeNumber').html(data.successList.length);
            //     }
            //     $.Huimodalalert('成功导入'+q+'条商品！第{'+failSpu+'}行SPU编码和第{'+failSku+'}行SKU编码的商品:不存在!',3000);
            // }
            // else if(data.failSpuId.length != 0 && data.failSkuId.length == 0){
            //     console.log(456)
            //     for(var i=0;i<data.failSpuId.length;i++){
            //         failSpu.push(data.failSpuId[i]);
            //     };
            //     if($('.current').text()=='适用商品'){
            //         $('#applNumber').html(data.successList.length);
            //     }
            //     if($('.current').text()=='排除商品'){
            //         $('#removeNumber').html(data.successList.length);
            //     }
            //     $.Huimodalalert('成功导入'+q+'条商品！第{'+failSpu+'}行SPU编码的商品:不存在!',3000);
            // }
            // else if(data.failSpuId.length == 0 && data.failSkuId.length != 0){
            //     console.log(789)
            //     for(var i=0;i<data.failSkuId.length;i++){
            //         failSku.push(data.failSkuId[i]);
            //     };
            //     if($('.current').text()=='适用商品'){
            //         $('#applNumber').html(data.successList.length);
            //     }
            //     if($('.current').text()=='排除商品'){
            //         $('#removeNumber').html(data.successList.length);
            //     }
            //     $.Huimodalalert('成功导入'+q+'条商品！第{'+failSku+'}行SKU编码的商品:不存在!',3000);
            // }
        }
    });
}

function downloadTem() {
    $.ajax({
        url: "http://192.168.7.100:8087/downloadProductExcelTemplate",
        cache: false,  //禁用缓存
        type: "GET",
        data: "",
        success: function (data) {
            console.log(data);
        }
    });
}

//var test=0  //点击加号即出现对应品牌
function selectBrand() {
    //test=1
    $("#selectInp").css({
        display: "block"
    });

    $("#aclick").css({
        display: "none"
    });
    $("#tuan").html("<li>无数据</li> ");
    //myFunction()
    $("#tuan").show();

    $("#username").val("")

}

function brand_cancel() {
    // if(sessionStorage.getItem("brand_name")){
    //     location.replace(location.href)
    //     $("#selectInp").css({
    //         display:"none"
    //     });
    // }else {
    //     sessionStorage.removeItem("brand_name");
    //     sessionStorage.removeItem("brand_id");
    //     $("#selectInp").css({
    //         display:"none"
    //     });
    // }
    $("#selectInp").css({
        display: "none"
    });
    $("#aclick").css({
        display: "inline"
    });

}

function brand_sure() {
    sessionStorage.setItem("brand_name", brandName);
    sessionStorage.setItem("brand_id", brandId);
    $("#selectInp").css({
        display: "none"
    });
    $("#aclick").css({
        display: "inline"
    });

    // if ($("#append div[name]").length%2==0&&$("#append div[name]").length!=0){
    //     $("#append").append("<br><div class=\"btn btn-secondary radius size-S ml-10\" name=\"brandMsg\">\n" +
    //         "<span >"+brandName+"</span>\n" +
    //         "<a id="+brandId+" onclick=\"removeModel(this.id)\" style='color: #ffffff'>×</a>\n" +
    //         "</div>");
    // }else{
    $("#append").append("<div class=\"btn btn-secondary radius size-S ml-10\" name=\"brandMsg\">\n" +
        "<span >" + brandName + "</span>\n" +
        "<a id=" + brandId + " onclick=\"removeModel(this.id)\" style='color: #ffffff'>×</a>\n" +
        "</div>");
    // }


    // addBrandName();
}

//removeModel()
function removeModel(id) {
    $("#" + id).parent().remove();
}

//渲染品牌名称
function addBrandName() {
    if (sessionStorage.getItem("brand_name")) {
        $("#brandName").html(sessionStorage.getItem("brand_name"));
        $("#brandId").html(sessionStorage.getItem("brand_id"));
    }
}

function myFunction() {
    //点击+号即出现品牌名
    //var brandNameTest=""

    // if(test){
    //     brandNameTest=""
    // }else
    // {
    //     brandNameTest=$("#username").val()
    // }

    $.ajax({
        url: root.goodsInter.goods_getListBrand,
        cache: false,  //禁用缓存
        type: "POST",
        data: {
            brandName: $("#username").val()
        },
        success: function (data) {
            console.log(data)
            var jsonObj = data.ListBrand;
            if (jsonObj == "") {
                $("#tuan").html("<li>无数据</li> ");
            } else {
                $("#tuan").show();
                console.log(data.ListBrand)
                console.log(jsonObj.length + "!!!!!!!");
                var optionstring = "";
                for (var i = 0; i < jsonObj.length; i++) {
                    console.log(jsonObj[0])
                    optionstring += "<li onclick='addCodeFun(" + jsonObj[i].brandId + ",\"" + jsonObj[i].brandName + "\")'>" + jsonObj[i].brandName + "</li>";
                }
                $("#tuan").html(optionstring);
            }
        }
    });
}

var brandName = "";
var brandId = "";

function addCodeFun(id, brand_name) {
    $("#tuan").hide();
    $("#username").val(brand_name)
    brandName = brand_name;
    brandId = id;
}


function upLoadFile() {
    $("#fileUp").trigger('click');
}

function getNum(Val) {
    console.log(Val);
}

/*$(document).bind("click",function(e){
   console.log(e.target)
})*/
/*分类设置--------------------------------------------------------------------------------------------------------------*/
// var map = {}; // Map map = new HashMap();
// map[key] = value; // map.put(key, value);
// var value = map[key]; // Object value = map.get(key);
// var has = key in map; // boolean has = map.containsKey(key);
// delete map[key]; // map.remove(key);
// // 遍历
// for(key in map){
//     $.Huimodalalert(key + map[key]);
// }
// //长度判断
// var length  = Object.keys(map).length
//A计划
var map1 = {};
var map2 = {};
var map3 = [];
var map4 = [];
var firstLevel = [];
var secondLevel = [];
var thirdLevel = [];
var secondLevelSelect = [];
var first = [];
var second = [];
var third = [];
var fourth = [];
var thirdVal = [];
var length2 = 0;
var thirdToFirst = [];
var selectall = 1;

//全选
$("#selectAll").click(function () {
    var Ids = 0;
    first = [];
    if (selectall % 2 == 1) {
        $("input[name='classification01']").each(function () {
            $(this).prop("checked", true);
            Ids = parseInt(this.id);
            first.push(Ids);
            map1[this.id] = {};
        });
        $("#classDiv2").css({
            display: "none"
        });
        $(".removeEl2").remove();
        $("#classDiv3").css({
            display: "none"
        });
        // 添加四级分类
        $('#classDiv4').hide();
        selectall++;
    } else {
        $("input[name='classification01']").each(function () {
            $(this).prop("checked", false);
        });
        $("input[name='classification02']").each(function () {
            $(this).prop("checked", false);
        });
        $("input[name='classification03']").each(function () {
            $(this).prop("checked", false);
        });
        selectall++;
        firstLevel = [];
        secondLevel = [];
        thirdLevel = [];
        map1 = {};
    }
});

//重置
$('#selectReset').click(function () {

    secondLevelSelect = [];
    second = [];
    third = [];
    thirdVal = [];
    oldparentId = [];
    c = [];
    oldparentid = 0;
    $("#classDiv2").css({
        display: "none"
    });
    $(".removeEl2").remove();
    $("#classDiv3").css({
        display: "none"
    });
    $(".removeEl3").remove();

    // 添加四级分类
    $('#classDiv4').hide();
    $(".removeEl4").remove();

    $("input[name='classification01']").each(function () {
        $(this).prop("checked", false);
    });
    $("input[name='classification02']").each(function () {
        $(this).prop("checked", false);
    });
    $("input[name='classification03']").each(function () {
        $(this).prop("checked", false);
    });
    $("input[name='applRange']").each(function () {
        $(this).prop("checked", false);
    });
    $("input[name='applCate']").each(function () {
        $(this).prop("checked", false);
    });


    // map.put("c","cccccccccccccc");
    // map.remove("b");
});

function makeMap() {
    firstLevel = [];
    for (key1 in map1) {
        thirdToFirst = [];
        var lll = 0;
        for (var i in map1[key1]) {
            lll++
        }
        if (lll == 0) {
            firstLevel.push(key1);
        }
        for (key2 in map1[key1]) {
            cateThird(key1, key2);
            cateSecond(key1, key2);
        }
        // console.log(map1);
        // console.log({"1":firstLevel, "2":secondLevel, "3":thirdLevel})

    }
    console.log({"1": firstLevel, "2": secondLevel, "3": thirdLevel})
    /*for(var i=0;i< firstLevel.length;i++){
        firstLevel[i]=parseInt(firstLevel[i])
    }*/
}

// 判断一级二级关系
function cateSecond(key1, key2) {
    $.ajax({
        /* url: root.goodsInter.goods_subCategory,*/
        url: root.goodsInter.goods_getProductCategory,
        cache: false,  //禁用缓存
        type: "POST",
        dataType: 'json',
        async: false,
        // contentType: "application/json",
        data: {
            category_id: key1,
        },
        success: function (data) {
            var length = 0;
            for (var i in map1[key1]) {
                length++
            }
            if (length == data.data.length) {
                if (thirdToFirst.length == 0 && $.inArray(key1, firstLevel) == -1) {
                    firstLevel.push(key1);
                } else if (thirdToFirst.length != 0) {
                    if ($.inArray(key2, secondLevel) == -1) {
                    }
                }
            } else if (length == 0) {
                firstLevel.push(key1);
            } else {
                if ($.inArray(key2, secondLevel) == -1) {
                    secondLevel.push(key2);
                }
            }
            //判断二级三级关系
            $.ajax({
                url: root.goodsInter.goods_subCategory,
                cache: false,  //禁用缓存
                type: "POST",
                dataType: 'json',
                async: false,
                // contentType: "application/json",
                data: {
                    category_id: key2,
                },
                success: function (data) {
                    if (data.data.length != map1[key1][key2].length && map1[key1][key2].length != 0) {
                        secondLevel.remove(key2);
                    }
                },
                error: function () {
                    console.log('失败')
                }
            })
        },
        error: function () {
            console.log('失败')
        }
    })
}


// 判断一级三级关系
function cateThird(key1, key2) {
    $.ajax({
        url: root.goodsInter.goods_subCategory,
        cache: false,  //禁用缓存
        type: "POST",
        dataType: 'json',
        async: false,
        // contentType: "application/json",
        data: {
            category_id: key2,
        },
        success: function (data) {
            if (map1[key1][key2].length != 0 && map1[key1][key2].length != data.data.length) {
                thirdToFirst = thirdToFirst.concat(map1[key1][key2]);
                for (var i = 0; i < map1[key1][key2].length; i++) {
                    if ($.inArray(map1[key1][key2][i], thirdLevel == -1)) {
                        thirdLevel.push(map1[key1][key2][i])
                    }
                }
            }
        },
        error: function () {
            console.log('失败')
        }
    })
}


//数组删除某个元素
Array.prototype.indexOf = function (val) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == val) return i;
    }
    return -1;
};
Array.prototype.remove = function (val) {
    var index = this.indexOf(val);
    if (index > -1) {
        this.splice(index, 1);
    }
};
//字符串以什么结尾
String.prototype.endWith = function (endStr) {
    var d = this.length - endStr.length;
    return (d >= 0 && this.lastIndexOf(endStr) == d);
};

map_times = {};
/*var firstlength = 0;*/
var newArr = []

function goods_listCategory1() {
    var user_data = {
        parentUuid: 'systemUuid',
        levelNum: 1
    }
    var succCallBack = function (data) {
        if (data.retStatus == 1) {
            var result = data.retData;
            var str = '';
            if (result.length <= 0) {
                $("#classSet1").append('<li class="clearfix pl-10 mb-5">无数据</li>');
            } else {
                $.each(result, function (index, item) {
                    map_times[item.categoryUuid] = 0;
                    newArr.push(item.categoryUuid);
                    str += '<li class="clearfix pl-10 mb-5"><div class="f-l pd-5">' +
                        '<input id="' + item.categoryUuid + '" onclick="nextbox1(this,' + item.categoryNo + ',' + item.categoryUuid + ')" name="classification01" type="checkbox" value="' + item.categoryUuid + '" class="checkbox" />' +
                        '<label id="' + item.categoryUuid + 'q" class="pl-10"  onclick="isFirstClick(2,' + item.categoryUuid + ',this)">' + item.categoryNo + ' ' + item.categoryName + '</label></div>' +
                        '<div class="f-r"><a onclick="firstClassAdd(\'#firstClassInp2\',2,1,' + item.categoryUuid + ',' + item.categoryNo + ')" ></a>' +
                        '<a href="##" onclick="eidtInpShow(\'#firstClassInp1\',\'#addLabel1\',' + item.categoryNo + ',\'.firstClassId1\',\'' + data.data[i].category_name + '\',' + item.categoryUuid + ')" "></a>' +
                        '<a href="##" onclick="deleteGoodsClass(' + item.categoryUuid + ')"></a></div></li>'
                })
                $("#classSet1").html(str)
            }
        }
        pageShow()
        /*         var  firstlength = data.data.length;
                if (data.data.length == 0) {

                } else {

                    for (var i = 0; i < data.data.length; i++) {
                        map_times[data.data[i].category_id] = 0;
                        $("#classSet1").append('<li class="clearfix pl-10 mb-5"><div class="f-l pd-5">' +
                            '<input id="' + data.data[i].category_id + '" onclick="nextbox1(this,' + data.data[i].category_code + ',' + data.data[i].category_id + ')" name="classification01" type="checkbox" value="' + data.data[i].category_id + '" class="checkbox" />' +
                            '<label id="' + data.data[i].category_id + 'q" class="pl-10"  onclick="isFirstClick(2,' + data.data[i].category_id + ',this)">' + data.data[i].category_code + ' ' + data.data[i].category_name + '</label></div>' +
                            '<div class="f-r"><a onclick="firstClassAdd(\'#firstClassInp2\',2,1,' + data.data[i].category_id + ',' + data.data[i].category_code + ')" ></a>' +
                            '<a href="##" onclick="eidtInpShow(\'#firstClassInp1\',\'#addLabel1\',' + data.data[i].category_code + ',\'.firstClassId1\',\'' + data.data[i].category_name + '\',' + data.data[i].category_id + ')" "></a>' +
                            '<a href="##" onclick="deleteGoodsClass(' + data.data[i].category_id + ')"></a></div></li>')
                        newArr.push(data.data[i].category_id)
                    }
                }*/

    };
    return web.doAjax(root.goodsInter.goods_getProductCategory, 'post', user_data, succCallBack);
}


/* 一进来需要 请求回显接口 */
//不限
$("#box-1").attr("checked", "checked");
//不限
$("#xz-1").attr("checked", "checked");
//不限
$("#qd-1").attr("checked", "checked");
var doc = 0
var show = 0
var arr = [];
var arri = []
var arrii = []
var sedArr = [];
var thArr = [];

var sedCateIds = ''
var sedelses = ''
var thirIds = ''
var oneIds = ''

//var customer="XN00000002";
function pageShow() {

    //var formData = new FormData();
    // formData.append("customerId","CN00000008")
    //formData.append("customerId",sessionStorage.getItem("customerid"))
    $.ajax({
        url: root.dockingplatform.selectChannelProductRule,
        // 告诉jQuery不要去处理发送的数据
        // 告诉jQuery不要去设置Content-Type请求头
        //contentType : false,
        cache: false,
        dataType: "json",
        type: "POST",
        //data: formData,
        data: {
            customerId: sessionStorage.getItem("customerid")
        },
        success: function (data) {
            console.log(data);
            //回显
            if (data.errorCode == "0" && data.data != null) {
                $("#hide").hide();
                show = 1;

                oneIds = data.data.firstCategoryIds
                /*sedelses=data.data.secondCategoryParam*/
                sedelses = data.data.secondCategoryIds;
                sedCateIds = data.data.sedCateId
                /*thirIds=data.data.thirdCategoryParam*/
                thirIds = data.data.thirdCategoryIds


                //固定区域切换
                $("#getArea").attr("disabled", "disabled")
                //区域
                $("#getArea").val(data.data.saleAreaId)
                //一级品类id串
                arr = data.data.firstCategoryList

                sedArr = data.data.secondCategoryList

                thArr = data.data.thirdCategoryList

                $.each(sedArr, function (i, newItem) {
                    arri.push(newItem.categoryId)
                })

                $.each(thArr, function (i, newItem) {
                    arrii.push(newItem.categoryId)
                })

                if (arr.length != 0) {
                    doc = 1
                }

                $.each(arr, function (i, newItem) {
                    console.log(newItem.categoryId)
                    $.each(newArr, function (i, item) {
                        if (newItem.categoryId == item) {
                            $("#" + item).attr("checked", "checked");
                            $("#" + item).attr('disabled', 'disabled');
                        } else {
                            $("#" + item).attr('disabled', 'disabled');
                        }
                    })
                })

                //品类
                if (data.data.choiceCategoryType == 1) {
                    //适用
                    $("#radio-7").attr("checked", "checked");
                } else if (data.data.choiceCategoryType == 2) {
                    //排除
                    $("#radio-8").attr("checked", "checked");
                }
                //品牌
                if (data.data.choiceBrandType == 1) {
                    //适用
                    $("#box-2").attr("checked", "checked");
                } else if (data.data.choiceBrandType == 2) {
                    //排除
                    $("#box-3").attr("checked", "checked");
                } else {
                    //不限
                    $("#box-1").attr("checked", "checked");
                }

                console.log(data.data.brandList)
                if (data.data.brandList != null) {
                    $.each(data.data.brandList, function (i, item) {
                        $("#append").append("<div class=\"btn btn-secondary radius size-S ml-10\" name=\"brandMsg\">\n" +
                            "<span >" + item.brandName + "</span>\n" +
                            "<a id=" + item.brandId + " onclick=\"removeModel(this.id)\" style='color: #ffffff'>×</a>\n" +
                            "</div>");
                    })
                }
                //限制类
                if (data.data.limitCategoryType == 1) {
                    //适用
                    $("#xz-2").attr("checked", "checked");
                } else if (data.data.limitCategoryType == 2) {
                    //排除
                    $("#xz-3").attr("checked", "checked");
                } else {
                    //不限
                    $("#xz-1").attr("checked", "checked");
                }
                //起订量
                if (data.data.miniOrderType == 1) {
                    //适用
                    $("#qd-2").attr("checked", "checked");
                } else if (data.data.miniOrderType == 2) {
                    //排除
                    $("#qd-3").attr("checked", "checked");
                } else {
                    //不限
                    $("#qd-1").attr("checked", "checked");
                }
                //导入的商品
                $("#spuText").val(data.data.spuCodesInclude.replace(',', ';'));
                $('#skuText').val(data.data.skuCodesInclude.replace(',', ';'));
                $("#spuText1").val(data.data.spuCodesExclude.replace(',', ';'));
                $('#skuText1').val(data.data.skuCodesExclude.replace(',', ';'));


            } else {
                $("#thirdCategory").hide();
            }
        }
    });
}

function nextbox1(that, valCode, parentId) {
    var Val = parseInt(that.value);
    if (that.checked) {
        map1[Val] = {};
    } else {
        $("#classDiv2").css({
            display: "none"
        });
        $("#classDiv3").css({
            display: "none"
        });
        // 添加四级分类
        $('#classDiv4').hide();
        delete map1[Val];
    }
    map_times[parentId] = 2;
    goods_listCategory2(2, Val, that.nextElementSibling)//渲染二级类
}

var oldparentId = 0;

function goods_listCategory2(classNum, parentId, e) {
    if (e.previousElementSibling.checked) {
        var checkbox = e.previousElementSibling;
        oldparentId = parentId;
        $(".removeEl2").remove();
        $("#classDiv2").css({
            display: "block"
        });
        $("#classDiv3").css({
            display: "none"
        });
        // 四级分类
        $('#classDiv4').hide();
        var user_data = {
            /*category_id: parentId*/
            parentUuid: parentId,
            levelNum: classNum
        }
        var succCallBack = function (data) {
            if (data.retStatus == 1) {
                var result = data.retData;
                if (result.length <= 0) {
                    $("#classSet2").append('<li class="removeEl2 clearfix pl-10 mb-5">无数据</li>')
                } else {
                    $.each(result, function (index, item) {
                        var categoryId = item.categoryUuid;
                        var categoryNo = item.categoryNo;
                        var categoryName = item.categoryName;
                        if (show == 1) {
                            if ($.inArray(categoryId, arri) != -1) {
                                $("#classSet2").append('<li class="removeEl2 clearfix pl-10 mb-5"><div class="f-l pd-5" ><input onclick="nextbox2(this,' + categoryNo + ',' + parentId + ')" name="classification02" type="checkbox" value="' + categoryId + '" disabled="disabled" checked="checked" class="" /><label class="pl-10 secondlevel" onclick="goods_listCategory3(3,' + categoryId + ',' + parentId + ',this)" >' + categoryNo + ' ' + categoryName + '</label></div></li>')
                            } else {
                                $("#classSet2").append('<li class="removeEl2 clearfix pl-10 mb-5"><div class="f-l pd-5" ><input onclick="nextbox2(this,' + categoryNo + ',' + parentId + ')" name="classification02" type="checkbox" value="' + categoryId + '" disabled="disabled" class="" /><label class="pl-10 secondlevel" onclick="goods_listCategory3(3,' + categoryId + ',' + parentId + ',this)" >' + categoryNo + ' ' + categoryName + '</label></div></li>')
                            }
                        } else {
                            if (checkbox.checked) {
                                if ($.inArray(categoryId, second) != -1) {
                                    $("#classSet2").append('<li class="removeEl2 clearfix pl-10 mb-5"><div class="f-l pd-5"><input onclick="nextbox2(this,' + categoryNo + ',' + parentId + ')"  name="classification02" type="checkbox" value="' + categoryId + '"  class="" /><label class="pl-10 secondlevel" onclick="goods_listCategory3(3,' + categoryId + ',' + parentId + ',this)">' + categoryNo + ' ' + categoryName + '</label></div></li>')
                                } else {
                                    $("#classSet2").append('<li class="removeEl2 clearfix pl-10 mb-5"><div class="f-l pd-5" ><input onclick="nextbox2(this,' + categoryNo + ',' + parentId + ')" name="classification02" type="checkbox" value="' + categoryId + '" checked="checked" class="" /><label class="pl-10 secondlevel" onclick="goods_listCategory3(3,' + categoryId + ',' + parentId + ',this)" >' + categoryNo + ' ' + categoryName + '</label></div></li>')
                                }
                                $("input[name='classification02']").each(function () {
                                    if (this.checked) {
                                        map2[this.value] = [];
                                    }
                                });
                                map1[parentId] = map2;
                                map2 = {};
                            } else {
                                $("#classSet2").append('<li class="removeEl2 clearfix pl-10 mb-5"><div class="f-l pd-5"><input onclick="nextbox2(this,' + categoryNo + ',' + parentId + ')"  name="classification02" type="checkbox" value="' + categoryId + '"  class="" /><label class="pl-10 secondlevel" onclick="goods_listCategory3(3,' + categoryId + ',' + parentId + ',this)">' + categoryNo + ' ' + categoryName + '</label></div></li>')
                            }
                        }

                    })
                }
            }


            /*     if (data.data.length == 0) {
                     $("#classSet2").append('<li class="removeEl2 clearfix pl-10 mb-5">无数据</li>');
                 } else {
                     //有数据
                     for (var i = 0; i < data.data.length; i++) {
                         var categoryId = data.data[i].category_id;
                         if (show == 1) {
                             console.log(arri)
                             if ($.inArray(categoryId, arri) != -1) {
                                 $("#classSet2").append('<li class="removeEl2 clearfix pl-10 mb-5"><div class="f-l pd-5" ><input onclick="nextbox2(this,' + data.data[i].category_code + ',' + parentId + ')" name="classification02" type="checkbox" value="' + data.data[i].category_id + '" disabled="disabled" checked="checked" class="" /><label class="pl-10 secondlevel" onclick="goods_listCategory3(3,' + data.data[i].category_id + ',' + parentId + ',this)" >' + data.data[i].category_code + ' ' + data.data[i].category_name + '</label></div></li>')
                             } else {
                                 $("#classSet2").append('<li class="removeEl2 clearfix pl-10 mb-5"><div class="f-l pd-5" ><input onclick="nextbox2(this,' + data.data[i].category_code + ',' + parentId + ')" name="classification02" type="checkbox" value="' + data.data[i].category_id + '" disabled="disabled" class="" /><label class="pl-10 secondlevel" onclick="goods_listCategory3(3,' + data.data[i].category_id + ',' + parentId + ',this)" >' + data.data[i].category_code + ' ' + data.data[i].category_name + '</label></div></li>')
                             }
                         } else {
                             if (checkbox.checked) {
                                 if ($.inArray(categoryId, second) != -1) {
                                     $("#classSet2").append('<li class="removeEl2 clearfix pl-10 mb-5"><div class="f-l pd-5"><input onclick="nextbox2(this,' + data.data[i].category_code + ',' + parentId + ')"  name="classification02" type="checkbox" value="' + data.data[i].category_id + '"  class="" /><label class="pl-10 secondlevel" onclick="goods_listCategory3(3,' + data.data[i].category_id + ',' + parentId + ',this)">' + data.data[i].category_code + ' ' + data.data[i].category_name + '</label></div></li>')
                                 } else {
                                     $("#classSet2").append('<li class="removeEl2 clearfix pl-10 mb-5"><div class="f-l pd-5" ><input onclick="nextbox2(this,' + data.data[i].category_code + ',' + parentId + ')" name="classification02" type="checkbox" value="' + data.data[i].category_id + '" checked="checked" class="" /><label class="pl-10 secondlevel" onclick="goods_listCategory3(3,' + data.data[i].category_id + ',' + parentId + ',this)" >' + data.data[i].category_code + ' ' + data.data[i].category_name + '</label></div></li>')
                                 }
                                 $("input[name='classification02']").each(function () {
                                     if (this.checked) {
                                         map2[this.value] = [];
                                     }
                                 });
                                 map1[parentId] = map2;
                                 map2 = {};
                             } else {
                                 $("#classSet2").append('<li class="removeEl2 clearfix pl-10 mb-5"><div class="f-l pd-5"><input onclick="nextbox2(this,' + data.data[i].category_code + ',' + parentId + ')"  name="classification02" type="checkbox" value="' + data.data[i].category_id + '"  class="" /><label class="pl-10 secondlevel" onclick="goods_listCategory3(3,' + data.data[i].category_id + ',' + parentId + ',this)">' + data.data[i].category_code + ' ' + data.data[i].category_name + '</label></div></li>')
                             }
                         }
                     }
                 }*/
        };
        return web.doAjax(root.goodsInter.goods_getProductCategory, 'post', user_data, succCallBack);
    }
}

function isFirstClick(classNum, parentId, e) {
    if (e.previousElementSibling.checked) {

        map_times[parentId]++;
        console.log("aaaa" + map_times[parentId])
        if (map_times[parentId] == 1) {
            goods_listCategory2(classNum, parentId, e)
            map_times[parentId]++
        } else {
            goods_listCategory22(classNum, parentId, e)
        }
    }
}

function goods_listCategory22(classNum, parentId, e) {
    if (e.previousElementSibling.checked) {
        var checkbox = e.previousElementSibling;
        oldparentId = parentId;
        $(".removeEl2").remove();
        $("#classDiv2").css({
            display: "block"
        });
        $("#classDiv3").css({
            display: "none"
        });
        // 四级分类
        $('#classDiv4').hide();
        var user_data = {
            category_id: parentId
        }
        var succCallBack = function (data, status, response) {
            if (data.data.length == 0) {
                $("#classSet2").append('<li class="removeEl2 clearfix pl-10 mb-5">无数据</li>');
            } else {
                //有数据
                for (var i = 0; i < data.data.length; i++) {
                    var categoryId = data.data[i].category_id;
                    if (show == 1) {
                        console.log(arri)
                        if ($.inArray(categoryId, arri) != -1) {
                            $("#classSet2").append('<li class="removeEl2 clearfix pl-10 mb-5"><div class="f-l pd-5" ><input onclick="nextbox2(this,' + data.data[i].category_code + ',' + parentId + ')" name="classification02" type="checkbox" value="' + data.data[i].category_id + '" disabled="disabled" checked="checked" class="" /><label class="pl-10 secondlevel" onclick="goods_listCategory3(3,' + data.data[i].category_id + ',' + parentId + ',this)" >' + data.data[i].category_code + ' ' + data.data[i].category_name + '</label></div></li>')
                        } else {
                            $("#classSet2").append('<li class="removeEl2 clearfix pl-10 mb-5"><div class="f-l pd-5" ><input onclick="nextbox2(this,' + data.data[i].category_code + ',' + parentId + ')" name="classification02" type="checkbox" value="' + data.data[i].category_id + '" disabled="disabled" class="" /><label class="pl-10 secondlevel" onclick="goods_listCategory3(3,' + data.data[i].category_id + ',' + parentId + ',this)" >' + data.data[i].category_code + ' ' + data.data[i].category_name + '</label></div></li>')
                        }
                    } else {
                        if (checkbox.checked) {
                            if ($.inArray(categoryId, second) != -1) {
                                $("#classSet2").append('<li class="removeEl2 clearfix pl-10 mb-5"><div class="f-l pd-5"><input onclick="nextbox2(this,' + data.data[i].category_code + ',' + parentId + ')"  name="classification02" type="checkbox" value="' + data.data[i].category_id + '"  class="" /><label class="pl-10 secondlevel" onclick="goods_listCategory3(3,' + data.data[i].category_id + ',' + parentId + ',this)">' + data.data[i].category_code + ' ' + data.data[i].category_name + '</label></div></li>')
                            } else {
                                $("#classSet2").append('<li class="removeEl2 clearfix pl-10 mb-5"><div class="f-l pd-5" ><input onclick="nextbox2(this,' + data.data[i].category_code + ',' + parentId + ')" name="classification02" type="checkbox" value="' + data.data[i].category_id + '" checked="checked" class="" /><label class="pl-10 secondlevel" onclick="goods_listCategory3(3,' + data.data[i].category_id + ',' + parentId + ',this)" >' + data.data[i].category_code + ' ' + data.data[i].category_name + '</label></div></li>')
                            }
                            $("input[name='classification02']").each(function () {
                                if (this.checked) {
                                    map2[this.value] = [];
                                }
                            });
                            /*map1[parentId]=map2;
                            map2={};*/
                        } else {
                            $("#classSet2").append('<li class="removeEl2 clearfix pl-10 mb-5"><div class="f-l pd-5"><input onclick="nextbox2(this,' + data.data[i].category_code + ',' + parentId + ')"  name="classification02" type="checkbox" value="' + data.data[i].category_id + '"  class="" /><label class="pl-10 secondlevel" onclick="goods_listCategory3(3,' + data.data[i].category_id + ',' + parentId + ',this)">' + data.data[i].category_code + ' ' + data.data[i].category_name + '</label></div></li>')
                        }
                    }
                }
            }
        };
        return web.doAjax(root.goodsInter.goods_subCategory, 'post', user_data, succCallBack);
    }
}


var oldparentIds = [];

function nextbox2(that, valCode, parentId) {
    var a = 0;
    var b = [];
    var Val = parseInt(that.value)
    /* if($.inArray(parentId,oldparentIds)==-1){
         map2 = new Map();
     }*/
    if (that.checked) {
        second.remove(Val)
        map1[parentId][Val] = [];
        $("input[name='classification02']").each(function () {
            if (this.checked) {
                a++
            }
        });
        var levelsecond = document.getElementsByName('classification02').length;
    } else {
        second.push(Val)
        delete map1[parentId][Val];
        $("#classDiv3").css({
            display: "none"
        });
        $("input[name='classification02']").each(function () {
            if (this.checked) {
                b.push(parseInt(this.value));
            }
        });
        b = [];
    }
}

function goods_listCategory3(classNum, parentId, grandParentId, e) {
    var checkbox = e.previousElementSibling;

    if (checkbox.checked) {
        $("#classDiv3").css({
            display: "block"
        });
        $(".removeEl3").remove();
        var user_data = {
            parentUuid:parentId,
            levelNum:classNum
        }
        var succCallBack = function (data, status, response) {
            if(data.retStatus == 1){
                var result = data.retData;
                if(result.length <= 0){
                    $("#classSet3").append('<li class="removeEl3 clearfix pl-10 mb-5">无数据</li>');
                }else {
                    for (var i = 0; i < result.length; i++) {
                        var categoryId = result.categoryUuid.toString();
                        var newCategoryId = result.categoryUuid;
                        var categoryNo = item.categoryNo;
                        var categoryName = item.categoryName;

                        if (show == 1) {
                            console.log(arrii)
                            if ($.inArray(newCategoryId, arrii) != -1) {
                                $("#classSet3").append('<li class="removeEl3 clearfix pl-10 mb-5"><div class="f-l pd-5" ><input onclick="nextbox3(this,' + categoryNo + ',' + parentId + ',' + grandParentId + ')"  name="classification03" type="checkbox" value="' + newCategoryId + '" disabled="disabled" checked="checked"  class="checkbox" /><label class="pl-10" onclick="goods_listCategory4(4,' + newCategoryId + ',' + grandParentId + ')">' + categoryNo + ' ' + categoryName + '</label></div></li>')
                            } else {
                                $("#classSet3").append('<li class="removeEl3 clearfix pl-10 mb-5"><div class="f-l pd-5" ><input onclick="nextbox3(this,' + categoryNo + ',' + parentId + ',' + grandParentId + ')"  name="classification03" type="checkbox" value="' + newCategoryId + '" disabled="disabled" class="checkbox" /><label class="pl-10" onclick="goods_listCategory4(4,' + newCategoryId + ',' + grandParentId + ')">' + categoryNo + ' ' + categoryName + '</label></div></li>')
                            }
                        } else {
                            if (checkbox.checked) {
                                console.log(third)
                                if ($.inArray(categoryId, third) != -1) {
                                    $("#classSet3").append('<li class="removeEl3 clearfix pl-10 mb-5"><div class="f-l pd-5" ><input onclick="nextbox3(this,' + categoryNo + ',' + parentId + ',' + grandParentId + ')"  name="classification03" type="checkbox" value="' + newCategoryId + '"  class="" /><label class="pl-10" onclick="goods_listCategory4(4,' + newCategoryId + ',' + grandParentId + ')">' + categoryNo + ' ' + categoryName + '</label></div></li>')
                                } else {
                                    $("#classSet3").append('<li class="removeEl3 clearfix pl-10 mb-5"><div class="f-l pd-5" ><input onclick="nextbox3(this,' + categoryNo + ',' + parentId + ',' + grandParentId + ')"  name="classification03" type="checkbox" value="' + newCategoryId + '" checked="checked" class="checkbox" /><label class="pl-10" onclick="goods_listCategory4(4,' + newCategoryId + ',' + grandParentId + ')">' + categoryNo + ' ' + categoryName + '</label></div></li>')
                                }
                            } else {
                                $("#classSet3").append('<li class="removeEl3 clearfix pl-10 mb-5"><div class="f-l pd-5" ><input onclick="nextbox3(this,' + categoryNo + ',' + parentId + ',' + grandParentId + ')"  name="classification03" type="checkbox" value="' + newCategoryId + '"  class="" /><label class="pl-10" onclick="goods_listCategory4(4,' + newCategoryId + ',' + grandParentId + ')">' + categoryNo + ' ' + categoryName + '</label></div></li>')
                            }
                        }
                    }
                    $("input[name='classification03']").each(function () {
                        if (this.checked) {
                            map3.push(this.value);
                        }
                    });
                    map1[grandParentId][parentId] = map3;
                    map3 = [];
                }
            }



        /*    if (data.data.length == 0) {
                $("#classSet3").append('<li class="removeEl3 clearfix pl-10 mb-5">无数据</li>');
            } else {
                for (var i = 0; i < data.data.length; i++) {
                    var categoryId = data.data[i].category_id.toString()
                    var newCategoryId = data.data[i].category_id
                    if (show == 1) {
                        console.log(arrii)
                        if ($.inArray(newCategoryId, arrii) != -1) {
                            $("#classSet3").append('<li class="removeEl3 clearfix pl-10 mb-5"><div class="f-l pd-5" ><input onclick="nextbox3(this,' + data.data[i].category_code + ',' + parentId + ',' + grandParentId + ')"  name="classification03" type="checkbox" value="' + data.data[i].category_id + '" disabled="disabled" checked="checked"  class="checkbox" /><label class="pl-10" onclick="goods_listCategory4(4,' + data.data[i].category_id + ',' + grandParentId + ')">' + data.data[i].category_code + ' ' + data.data[i].category_name + '</label></div></li>')
                        } else {
                            $("#classSet3").append('<li class="removeEl3 clearfix pl-10 mb-5"><div class="f-l pd-5" ><input onclick="nextbox3(this,' + data.data[i].category_code + ',' + parentId + ',' + grandParentId + ')"  name="classification03" type="checkbox" value="' + data.data[i].category_id + '" disabled="disabled" class="checkbox" /><label class="pl-10" onclick="goods_listCategory4(4,' + data.data[i].category_id + ',' + grandParentId + ')">' + data.data[i].category_code + ' ' + data.data[i].category_name + '</label></div></li>')
                        }
                    } else {
                        if (checkbox.checked) {
                            console.log(third)
                            if ($.inArray(categoryId, third) != -1) {
                                $("#classSet3").append('<li class="removeEl3 clearfix pl-10 mb-5"><div class="f-l pd-5" ><input onclick="nextbox3(this,' + data.data[i].category_code + ',' + parentId + ',' + grandParentId + ')"  name="classification03" type="checkbox" value="' + data.data[i].category_id + '"  class="" /><label class="pl-10" onclick="goods_listCategory4(4,' + data.data[i].category_id + ',' + grandParentId + ')">' + data.data[i].category_code + ' ' + data.data[i].category_name + '</label></div></li>')
                            } else {
                                $("#classSet3").append('<li class="removeEl3 clearfix pl-10 mb-5"><div class="f-l pd-5" ><input onclick="nextbox3(this,' + data.data[i].category_code + ',' + parentId + ',' + grandParentId + ')"  name="classification03" type="checkbox" value="' + data.data[i].category_id + '" checked="checked" class="checkbox" /><label class="pl-10" onclick="goods_listCategory4(4,' + data.data[i].category_id + ',' + grandParentId + ')">' + data.data[i].category_code + ' ' + data.data[i].category_name + '</label></div></li>')
                            }
                        } else {
                            $("#classSet3").append('<li class="removeEl3 clearfix pl-10 mb-5"><div class="f-l pd-5" ><input onclick="nextbox3(this,' + data.data[i].category_code + ',' + parentId + ',' + grandParentId + ')"  name="classification03" type="checkbox" value="' + data.data[i].category_id + '"  class="" /><label class="pl-10" onclick="goods_listCategory4(4,' + data.data[i].category_id + ',' + grandParentId + ')">' + data.data[i].category_code + ' ' + data.data[i].category_name + '</label></div></li>')
                        }
                    }
                }
                $("input[name='classification03']").each(function () {
                    if (this.checked) {
                        map3.push(this.value);
                    }
                });
                map1[grandParentId][parentId] = map3;
                map3 = [];
            }*/
        }
        return web.doAjax(root.goodsInter.goods_getProductCategory, 'post', user_data, succCallBack);
    }
}

function goods_listCategory4(classNum, parentId, grandParentId) {
    $("#classDiv4").css({
        display: "block"
    });
    $(".removeEl4").remove();
    var user_data = {
        parentUuid:parentId,
        levelNum:classNum
    }
    var succCallBack = function (data) {
        if (data.retStatus == 1) {
            var result = data.retData;
            if (result.length <= 0) {
                $("#classSet4").append('<li class="removeEl2 clearfix pl-10 mb-5">无数据</li>')
            } else {
                $.each(result, function (index, item) {
                    var categoryId = item.categoryUuid;
                    var categoryNo = item.categoryNo;
                    var categoryName = item.categoryName;
                    if (show == 1) {
                        if ($.inArray(categoryId, arri) != -1) {
                            $("#classSet4").append('<li class="removeEl2 clearfix pl-10 mb-5"><div class="f-l pd-5" ><input onclick="nextbox4(this,' + categoryNo + ',' + parentId + ')" name="classification04" type="checkbox" value="' + categoryId + '" disabled="disabled" checked="checked" class="" /><label class="pl-10 secondlevel" onclick="goods_listCategory3(3,' + categoryId + ',' + parentId + ',this)" >' + categoryNo + ' ' + categoryName + '</label></div></li>')
                        } else {
                            $("#classSet4").append('<li class="removeEl2 clearfix pl-10 mb-5"><div class="f-l pd-5" ><input onclick="nextbox4(this,' + categoryNo + ',' + parentId + ')" name="classification04" type="checkbox" value="' + categoryId + '" disabled="disabled" class="" /><label class="pl-10 secondlevel" onclick="goods_listCategory3(3,' + categoryId + ',' + parentId + ',this)" >' + categoryNo + ' ' + categoryName + '</label></div></li>')
                        }
                    } else {
                        if (checkbox.checked) {
                            if ($.inArray(categoryId, second) != -1) {
                                $("#classSet4").append('<li class="removeEl2 clearfix pl-10 mb-5"><div class="f-l pd-5"><input onclick="nextbox4(this,' + categoryNo + ',' + parentId + ')"  name="classification04" type="checkbox" value="' + categoryId + '"  class="" /><label class="pl-10 secondlevel" onclick="goods_listCategory3(3,' + categoryId + ',' + parentId + ',this)">' + categoryNo + ' ' + categoryName + '</label></div></li>')
                            } else {
                                $("#classSet4").append('<li class="removeEl2 clearfix pl-10 mb-5"><div class="f-l pd-5" ><input onclick="nextbox4(this,' + categoryNo + ',' + parentId + ')" name="classification04" type="checkbox" value="' + categoryId + '" checked="checked" class="" /><label class="pl-10 secondlevel" onclick="goods_listCategory3(3,' + categoryId + ',' + parentId + ',this)" >' + categoryNo + ' ' + categoryName + '</label></div></li>')
                            }
                            $("input[name='classification04']").each(function () {
                                if (this.checked) {
                                    map2[this.value] = [];
                                }
                            });
                            map1[parentId] = map2;
                            map2 = {};
                        } else {
                            $("#classSet4").append('<li class="removeEl2 clearfix pl-10 mb-5"><div class="f-l pd-5"><input onclick="nextbox4(this,' + categoryNo + ',' + parentId + ')"  name="classification04" type="checkbox" value="' + categoryId + '"  class="" /><label class="pl-10 secondlevel" onclick="goods_listCategory3(3,' + categoryId + ',' + parentId + ',this)">' + categoryNo + ' ' + categoryName + '</label></div></li>')
                        }
                    }

                })
            }
        }


        /*     if (data.data.length == 0) {
                 $("#classSet2").append('<li class="removeEl2 clearfix pl-10 mb-5">无数据</li>');
             } else {
                 //有数据
                 for (var i = 0; i < data.data.length; i++) {
                     var categoryId = data.data[i].category_id;
                     if (show == 1) {
                         console.log(arri)
                         if ($.inArray(categoryId, arri) != -1) {
                             $("#classSet2").append('<li class="removeEl2 clearfix pl-10 mb-5"><div class="f-l pd-5" ><input onclick="nextbox2(this,' + data.data[i].category_code + ',' + parentId + ')" name="classification02" type="checkbox" value="' + data.data[i].category_id + '" disabled="disabled" checked="checked" class="" /><label class="pl-10 secondlevel" onclick="goods_listCategory3(3,' + data.data[i].category_id + ',' + parentId + ',this)" >' + data.data[i].category_code + ' ' + data.data[i].category_name + '</label></div></li>')
                         } else {
                             $("#classSet2").append('<li class="removeEl2 clearfix pl-10 mb-5"><div class="f-l pd-5" ><input onclick="nextbox2(this,' + data.data[i].category_code + ',' + parentId + ')" name="classification02" type="checkbox" value="' + data.data[i].category_id + '" disabled="disabled" class="" /><label class="pl-10 secondlevel" onclick="goods_listCategory3(3,' + data.data[i].category_id + ',' + parentId + ',this)" >' + data.data[i].category_code + ' ' + data.data[i].category_name + '</label></div></li>')
                         }
                     } else {
                         if (checkbox.checked) {
                             if ($.inArray(categoryId, second) != -1) {
                                 $("#classSet2").append('<li class="removeEl2 clearfix pl-10 mb-5"><div class="f-l pd-5"><input onclick="nextbox2(this,' + data.data[i].category_code + ',' + parentId + ')"  name="classification02" type="checkbox" value="' + data.data[i].category_id + '"  class="" /><label class="pl-10 secondlevel" onclick="goods_listCategory3(3,' + data.data[i].category_id + ',' + parentId + ',this)">' + data.data[i].category_code + ' ' + data.data[i].category_name + '</label></div></li>')
                             } else {
                                 $("#classSet2").append('<li class="removeEl2 clearfix pl-10 mb-5"><div class="f-l pd-5" ><input onclick="nextbox2(this,' + data.data[i].category_code + ',' + parentId + ')" name="classification02" type="checkbox" value="' + data.data[i].category_id + '" checked="checked" class="" /><label class="pl-10 secondlevel" onclick="goods_listCategory3(3,' + data.data[i].category_id + ',' + parentId + ',this)" >' + data.data[i].category_code + ' ' + data.data[i].category_name + '</label></div></li>')
                             }
                             $("input[name='classification02']").each(function () {
                                 if (this.checked) {
                                     map2[this.value] = [];
                                 }
                             });
                             map1[parentId] = map2;
                             map2 = {};
                         } else {
                             $("#classSet2").append('<li class="removeEl2 clearfix pl-10 mb-5"><div class="f-l pd-5"><input onclick="nextbox2(this,' + data.data[i].category_code + ',' + parentId + ')"  name="classification02" type="checkbox" value="' + data.data[i].category_id + '"  class="" /><label class="pl-10 secondlevel" onclick="goods_listCategory3(3,' + data.data[i].category_id + ',' + parentId + ',this)">' + data.data[i].category_code + ' ' + data.data[i].category_name + '</label></div></li>')
                         }
                     }
                 }
             }*/
    };
    return web.doAjax(root.goodsInter.goods_getProductCategory, 'post', user_data, succCallBack);
}

function nextbox3(that, valCode, parentId, grandParentId) {
    var c = []
    var thirds = 0;
    var seconds = 0;
    if (that.checked) {
        $("input[name='classification03']").each(function () {
            if (this.checked) {
                map3.push(this.value);
                thirds++;
            }
        });
        $("input[name='classification02']").each(function () {
            if (this.checked) {
                seconds++;
            }
        });
        var levelthird = document.getElementsByName('classification03').length;
        var levelsecond = document.getElementsByName('classification02').length;

        var k = 0;
        for (key in map1[grandParentId][parentId]) {
            k++
        }
        map1[grandParentId][parentId] = map3;
        if (seconds == k) {
            map1[grandParentId][parentId] = [];
        }
        map3 = [];
        third.remove(that.value);
    } else {
        third.push(that.value)
        console.log(that.value)
        console.log(third)
        $("input[name='classification03']").each(function () {
            if (this.checked) {
                map3.push(this.value);
                c.push(parseInt(this.value))
            }
        });
        c = [];
        map1[grandParentId][parentId] = map3;
        map3 = [];
        /*thirdLevel.remove(that.value)*/
    }
    var thirdArray = [];
    /*$.ajax({
        url: root.goodsInter.goods_subCategory,
        cache: false,  //禁用缓存
        type: "POST",
        dataType: 'json',
        // contentType: "application/json",
        data: {
            category_id: parentId,
        },
        success: function (data) {
            console.log(data.data);
            var thirdSelect = [];
            var a = document.getElementById('classSet3');
            var b=a.getElementsByTagName('input');
            for(var i = 0; i < b.length; i++){
                if(b[i].checked)
                    thirdSelect.push(b[i].value);
            }
            console.log(thirdSelect);
            if(thirdSelect.length==data.data.length){
                for(var i in thirdSelect){
                   thirdLevel.remove(thirdSelect[i]);
                }
                if($.inArray(parentId,secondLevel)==-1){
                    secondLevel.push(parentId);
                }
            }
            else{
                for(var i in thirdSelect){
                    if($.inArray(thirdSelect[i],thirdLevel)==-1){
                        thirdLevel.push(thirdSelect[i]);
                    }
                }
                if($.inArray(parentId,secondLevel)!=-1){
                    secondLevel.remove(parentId);
                }
            }
            console.log(firstLevel)
            console.log(secondLevel)
            console.log(thirdLevel)
        },
        error: function () {
            console.log('失败')
        }
    })*/

}
function nextbox4(that, valCode, parentId, grandParentId) {
    var c = []
    var thirds = 0;
    var seconds = 0;
    var fourths = 0;
    if (that.checked) {
        $("input[name='classification04']").each(function () {
            if (this.checked) {
                map4.push(this.value);
                fourths++;
            }
        });
        $("input[name='classification03']").each(function () {
            if (this.checked) {
                thirds++;
            }
        });
        var levelthird = document.getElementsByName('classification04').length;
        var levelsecond = document.getElementsByName('classification03').length;

        var k = 0;
        for (key in map1[grandParentId][parentId]) {
            k++
        }
        map1[grandParentId][parentId] = map4;
        if (seconds == k) {
            map1[grandParentId][parentId] = [];
        }
        map4 = [];
        fourth.remove(that.value);
    } else {
        fourth.push(that.value)
        $("input[name='classification04']").each(function () {
            if (this.checked) {
                map4.push(this.value);
                c.push(parseInt(this.value))
            }
        });
        c = [];
        map1[grandParentId][parentId] = map4;
        map4 = [];
        /*thirdLevel.remove(that.value)*/
    }
    var thirdArray = [];
    /*$.ajax({
        url: root.goodsInter.goods_subCategory,
        cache: false,  //禁用缓存
        type: "POST",
        dataType: 'json',
        // contentType: "application/json",
        data: {
            category_id: parentId,
        },
        success: function (data) {
            console.log(data.data);
            var thirdSelect = [];
            var a = document.getElementById('classSet3');
            var b=a.getElementsByTagName('input');
            for(var i = 0; i < b.length; i++){
                if(b[i].checked)
                    thirdSelect.push(b[i].value);
            }
            console.log(thirdSelect);
            if(thirdSelect.length==data.data.length){
                for(var i in thirdSelect){
                   thirdLevel.remove(thirdSelect[i]);
                }
                if($.inArray(parentId,secondLevel)==-1){
                    secondLevel.push(parentId);
                }
            }
            else{
                for(var i in thirdSelect){
                    if($.inArray(thirdSelect[i],thirdLevel)==-1){
                        thirdLevel.push(thirdSelect[i]);
                    }
                }
                if($.inArray(parentId,secondLevel)!=-1){
                    secondLevel.remove(parentId);
                }
            }
            console.log(firstLevel)
            console.log(secondLevel)
            console.log(thirdLevel)
        },
        error: function () {
            console.log('失败')
        }
    })*/

}


//提交
var range = 0;
$("#coupon-add").validate({
    rules: {
        couponName: {
            required: true
        },
        amount: {
            required: true,
            checkBalance: true,
        },
        totalCount: {
            required: true,
            checkBalance: true,
        },
        demoradio1: {
            required: true
        },
        demoradio2: {
            required: true
        },
        applPort: {
            required: true
        },
        applCategoryIntroduce: {
            required: true
        },
        applCate: {
            // required:true
            required: function () {
                if ($('input:radio[name="applRange"]:checked').val() == 0 || $('input:radio[name="applRange"]:checked').val() == 1 || $('input:radio[name="applRange"]:checked').val() == 2) {
                    return true;
                } else {
                    return false;
                }
            }
        },
        useCondition: {
            required: function () {
                if ($('input:radio[name="demoradio1"]:checked').val() == 2) {
                    return true;
                } else {
                    return false;
                }
            },
            min: 1,
        },
        dayNumber: {
            required: function () {
                if ($('input:radio[name="demoradio2"]:checked').val() == 2) {
                    return true;
                } else {
                    return false;
                }
            }
        },
        logmin: {
            required: function () {
                if ($('input:radio[name="demoradio2"]:checked').val() == 1) {
                    return true;
                } else {
                    return false;
                }
            }
        },
        applCategoryIntroduce: {
            required: function () {
                if ($('#applCategoryIntroduce').val() != '') {
                    return true;
                } else {
                    return false;
                }
            }
        },
    },
    messages: {
        demoradio1: {
            required: "请选择"
        },
        demoradio2: {
            required: "请选择"
        },
        applPort: {
            required: "请选择"
        },
        // applRange:{
        //     required:"请选择"
        // },
        applCate: {
            required: "请选择"
        },
        dayNumber: {
            required: ""
        },
        useCondition: {
            required: ""
        },
        logmin: {
            required: ""
        }
    },
    errorPlacement: function (error, element) {//指定错误信息位置
        if (element.is(':radio') || element.is(':checkbox')) { //如果是radio或checkbox
            var eid = element.attr('name'); //获取元素的name属性
            error.appendTo(element.parent().prev()); //将错误信息添加当前元素的父结点后面
        } else {
            error.insertAfter(element);
        }
    },
    onkeyup: false,
    focusCleanup: false,
    success: function () {
        $('.tip').hide()
    },
    submitHandler: function () {
        if ($("#getArea").val() == 0) {
            $.Huimodalalert('请选择区域', 1000);
            return;
        }

        if ($("input[name=applCate]:checked").val() == undefined) {
            $.Huimodalalert('请选择品类', 1000);
            return;
        }


        makeMap();
        console.log(map1);


        if (show != 1) {
            var sedCateId = [];
            var sedelse = [];
            var thirId = [];
            var oneId = [];

            for (i in map1) {
                oneId.push(i);
                for (s in map1[i]) {
                    if (map1[i][s].length == 0) {
                        sedCateId.push(s);
                    } else {
                        sedelse.push(s);
                        $.each(map1[i][s], function (i, item) {
                            thirId.push(item);
                        })
                    }
                }
            }
            console.log(sedCateId);
            console.log(sedelse);
            console.log(thirId);
            console.log(oneId);
            sedCateIds = sedCateId.join(',')
            sedelses = sedelse.join(',')
            thirIds = thirId.join(',')
            oneIds = oneId.join(',')
        }


        if (oneIds.length == 0 && doc == 0) {
            $.Huimodalalert('请选择品类名称', 1000);
            return;
        }
        var brandIds = []

        $("#append a[id]").each(function () {
            brandIds.push($(this).attr("id"))
        })
        if ($("input[name=brand]:checked").val() != 0
            && brandIds.length == 0) {
            $.Huimodalalert('请添加品牌', 1000);
            return;
        }
        // if ($("input[name=brand]:checked").val()==0
        //     &&brandIds.length!=0){
        //     $.Huimodalalert('请重新选择品牌类型',1000);
        //     return;
        // }
        var noSend = 0
        $.each(brandIds, function (i, first) {
            $.each(brandIds, function (s, sed) {
                if (i == s) {
                    return true;
                }
                if (first == sed) {
                    $.Huimodalalert('品牌相同,请重试', 1000);
                    noSend = 1;
                }
            })
        })

        //保存提示
        if (noSend == 1) {
            return;
        }

        $.ajax({
            contentType: 'application/json',
            url: root.dockingplatform.insertChannelProductRule,
            cache: false,  //禁用缓存
            type: "POST",
            data: JSON.stringify({
                "channelProductRule": {
                    "customerId": sessionStorage.getItem("customerid"),
                    "customerName": sessionStorage.getItem("customerName"),
                    "saleAreaId": $("#getArea").find("option:selected").val(),
                    "saleAreaName": $("#getArea").find("option:selected").html(),
                    "choiceCategoryType": $("input[name=applCate]:checked").val(),
                    "firstCategoryIds": oneIds,
                    "secondCategoryIds": sedelses,
                    "thirdCategoryIds": thirIds,
                    "sedCateId": sedCateIds,
                    "choiceBrandType": $("input[name=brand]:checked").val(),//choiceBrandType
                    "choiceBrandIds": brandIds.join(','),
                    "limitCategoryType": $("input[name=xianzhi]:checked").val(),
                    "miniOrderType": $("input[name=qiding]:checked").val(),
                    "creationBy": '',
                    "lastModifiedBy": '',
                    "spuCodesInclude": $("#spuText").val(),
                    "skuCodesInclude": $("#skuText").val(),
                    "spuCodesExclude": $("#spuText1").val(),
                    "skuCodesExclude": $("#skuText1").val(),
                    "excludeThirdCategory": $("#excludeThirdCategory").val(),
                    "addThirdCategory": $("#addThirdCategory").val()
                }
            }),

            success: function (data) {
                console.log(firstLevel);
                console.log(secondLevel);
                console.log(thirdLevel);
                if (data.errorCode == 0) {
                    $.Huimodalalert('保存成功!请继续进行价格设置！    ', 3000);
                    document.body.scrollTop = document.documentElement.scrollTop = 0;
                    localStorage.removeItem("excludeThirdCategory");
                    localStorage.removeItem("addThirdCategory");

                    $.ajax({
                        contentType: 'application/json',
                        url: root.dockingplatform.editChannelProductSetted,
                        cache: false,  //禁用缓存
                        type: "POST",
                        data: JSON.stringify({
                            "id": sessionStorage.getItem("id"),
                            "productSetted": 20
                        })
                    })

                    setTimeout(function () {
                        var index = parent.location.reload();
                        parent.layer.close(index);
                    }, 700);
                } else if (data.errorCode == 1) {
                    // $("#bottm").attr("disabled", false);
                    // layer.confirm(data.msg, function(index){
                    //
                    //     layer.close(index);
                    // });
                    $.Huimodalalert(data.msg, 3000);
                }

            },
            error: function () {
                $.Huimodalalert('保存失败,请重新提交！', 2000);
            }
        });
    }
})

function check() {
    console.log(firstLevel);
    console.log(secondLevel);
    console.log(thirdLevel);

    console.log("range=" + range);

    console.log("firstlength=" + firstlength);
    console.log("firstLevel.length=" + firstLevel.length);

}

